<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2018/4/13
 * Time: 10:12
 */

namespace App\Http\Model\Api;


use App\Http\Base\BaseModel;
use App\Http\Utils\JUtil;
use App\Http\Utils\TokenUtils;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use PhpParser\Node\Expr\Array_;
use Symfony\Component\VarDumper\Tests\Fixture\DumbFoo;


class User extends BaseModel
{
    protected $table = 'user';

    protected $dateFormat = 'U';

    public function userInfo()
    {
        $username = request('username');
        $password = request('password');

    }

    /**
     * 登录
     * @return array
     */
    public function login()
    {
        $username = request('username');
        $password = request('password');

        $where['username'] = $username;
        $user = $this->where($where)->first();

        if (empty($user)) return JUtil::E('账号或密码错误');

        $check = Hash::check($password, $user->password);

        if (!$check) return JUtil::E('账号或密码错误');

        $id         = $user->id;
        $nickname   = $user->nickname;
        $sex        = $user->sex;
        $avatar     = $user->avatar;
        $mobile     = $user->mobile;
        $autograph  = $user->autograph;

        return $this->_createUserInfo($id, $username, $mobile, $nickname, $sex, null, $avatar, $autograph);
    }

    public function register()
    {
        $username = request('username');
        $password = request('password');

        // 添加数据
        $password = Hash::make($password);
        $this->username = $username;
        $this->password = $password;

        try {
            if (!$this->save()) return JUtil::SqlError();

            $id = $this->id;
            return $this->_createUserInfo($id, $username);

        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return JUtil::SqlError();
        }
    }


    /**
     * 创建用户缓存信息，并保存在缓存中，且返回json数据
     * @param $userId           @用户id
     * @param $username         @用户名
     * @param string $mobile    @用户手机号
     * @param string $nickname  @用户昵称
     * @param string $sex       @用户性别
     * @param string $birthday  @用户生日
     * @param string $avatar    @用户头像
     * @param string $autograph @用户简介
     * @return array            @用户返回的json数据
     */
    private function _createUserInfo($userId, $username, $mobile=null, $nickname = null,
                                     $sex = null,  $birthday=null,$avatar=null, $autograph=null)
    {
        $userCache = array(
            'userId'    => $userId,
            'username'  => $username,
            'mobile'    => $mobile,
            'nickname'  => $nickname,
            'sex'       => $sex,
            'birthday'  => $birthday,
            'avatar'    => $avatar,
            'autograph' => $autograph,
        );

        $token = TokenUtils::S($userCache);
        if (empty($token)) return JUtil::SqlError();

        unset($userCache['userId']);
        $userCache['token'] = $token;

        return JUtil::S('', $userCache);
    }

}